/**
 * @copyright Copyright (c) 2024 COOREAL. All rights reserved.
 * @description ArrowBuiler.ts
 * @author xuezejun @date 2025/07/01
 */

import * as GUI from '@babylonjs/gui';
/**
 * 提供一个箭头的构建器。
 */
export class ArrowBuiler {
  public static build(direction: string, x: number, y: number, w: number, h: number): GUI.Container | undefined {
    var container = new GUI.Container;
    container.width = w + 'px';
    container.height = h + 'px';

    var points: {
      x: number;
      y: number;
    }[] = [];
    var rotation = 0;
    var upperCase = direction.toUpperCase();
    switch (upperCase) {
      case 'N':
      case 'S':
      case 'W':
      case 'E':
        points = [
          { x: x, y: y - h * 0.5 },
          { x: x + w * 0.3, y: y - h * 0.5 },
          { x: x + w * 0.3, y: y },
          { x: x + w * 0.5, y: y },
          { x: x, y: y + h * 0.5 },
          { x: x - w * 0.5, y: y },
          { x: x - w * 0.3, y: y },
          { x: x - w * 0.3, y: y - h * 0.5 },
          { x: x, y: y - h * 0.5 },
        ];
        rotation = upperCase == 'E' ? Math.PI / 2 : upperCase == 'S' ? Math.PI : upperCase == 'W' ? Math.PI * 1.5 : 0;
        break;
      case 'NS':
      case 'SN':
      case 'WE':
      case 'EW':
        points = [
          { x: x, y: y - h * 0.5 },
          { x: x + w * 0.5, y: y - h * 0.5 * 0.5 },
          { x: x + w * 0.3, y: y - h * 0.5 * 0.5 },
          { x: x + w * 0.3, y: y + h * 0.5 * 0.5 },
          { x: x + w * 0.5, y: y + h * 0.5 * 0.5 },
          { x: x, y: y + h * 0.5 },

          { x: x - w * 0.5, y: y + h * 0.5 * 0.5 },
          { x: x - w * 0.3, y: y + h * 0.5 * 0.5 },
          { x: x - w * 0.3, y: y - h * 0.5 * 0.5 },
          { x: x - w * 0.5, y: y - h * 0.5 * 0.5 },
          { x: x, y: y - h * 0.5 },
        ];
        rotation = upperCase == 'WE' || upperCase == 'EW' ? Math.PI : 0;
        break;
      case 'EN':
      case 'NE':
      case 'WN':
      case 'NW':
      case 'ES':
      case 'SE':
      case 'WS':
      case 'SW':
        points = [
          { x: x, y: y - h * 0.5 },
          { x: x + w * 0.5, y: y - h * 0.5 * 0.5 },
          { x: x + w * 0.3, y: y - h * 0.5 * 0.5 },
          { x: x + w * 0.3, y: y + h * 0.5 * 0.5 },
          { x: x + w * 0.5, y: y + h * 0.5 * 0.5 },
          { x: x, y: y + h * 0.5 },

          { x: x - w * 0.5, y: y + h * 0.5 * 0.5 },
          { x: x - w * 0.3, y: y + h * 0.5 * 0.5 },
          { x: x - w * 0.3, y: y - h * 0.5 * 0.5 },
          { x: x - w * 0.5, y: y - h * 0.5 * 0.5 },
          { x: x, y: y - h * 0.5 },
        ];
        rotation =
          upperCase == 'EN' || upperCase == 'NE'
            ? Math.PI / 2
            : upperCase == 'ES' || upperCase == 'SE'
              ? Math.PI
              : upperCase == 'WN' || upperCase == 'NW'
                ? Math.PI * 1.5
                : 0;

        break;
      default:
        return undefined;
    }

    var arrow = new GUI.MultiLine();
    arrow.add(...points);
    arrow.setPadding(h,0,0,w);
    arrow.rotation = rotation;
    container.addControl(arrow);
    return container;
  }
}
